$(function(){
	var photoPath;
	$(".btn-close1").click(function(){
		$("#fileField").val("");
		$(".touxiang").css("box-shadow","none");
		$(".imgChange").css("display","block");
		$(".changeBtn, .btn-close1").css("display","none");
		$(".touxiang").css("height", "200px");
		$('#photo').imgAreaSelect({hide: 'true'});
		$("#photo").attr("src", photoPath);
	});
	$(".imgChange").click(function(){
		$("#fileField").click();
	});
	$("#imgKnow").click(function(){
		closeDialog("imgError");
	});
	$("#fileField").live("change", function () {
		photoPath = $("#photo").attr('src');
		var fileReader=new FileReader();
		fileReader.onloadend=function(e){
			var newPath = $("#fileField").val();
			var ext = newPath.substring(newPath.lastIndexOf(".") + 1, newPath.length).toUpperCase( );
			var extT = [ "JPG", "GIF", "JPEG", "PNG" ];
			if($.inArray(ext, extT) == -1){
				openDialog("imgError");
			}else{
				$("#photo").attr("src",e.target.result);
				$(".touxiang").css("box-shadow"," -5px 5px 15px 3px #2B2B2B");
				var pheight = $("#photo").height();
				if(pheight > 112){
					$('#photo').imgAreaSelect({ x1: 58, y1: 27, x2: 143, y2: 112 });
				}else{
					var h = pheight - 27;
					$('#photo').imgAreaSelect({ x1: 58, y1: 27, x2: (58 + h), y2: (27 + h)});
					$("#height").val(h);
					$("#width").val(h);
				}

				$(".touxiang").css("height", (pheight + 50));
				$(".btn-close1, .changeBtn").css("display", "block");
				$(".imgChange").css("display","none");
			}
		};
		fileReader.readAsDataURL(this.files[0]);
	})
	$('#photo').imgAreaSelect({aspectRatio: '1:1', handles: true,
	      fadeSpeed: 200, onSelectChange: preview });
	$(".changeBtn").click(function(){
		ajaxFileUpload("fileField", $("#x").val(), $("#y").val(), $("#width").val(), $("#height").val())
	});
	var name = "";
	var age = "";
	var telephone = "";
	$("#edit").click(function(){
		var vlu = ($(this).html() == "保存" ? "编辑" : "保存");
		$(this).html(vlu);
		name = $("#name").val();
		age = $("#age").val();
		telephone = $("#telephone").val();
		if(vlu == "保存"){
			$(".editInfo").removeAttr("disabled");
		}else{
			var major = $("#major").val();
			var degree = $("#degree").val();
			var workplace = $("#workplace").val();
			var intruduction = $("#introduction").val();
			$(".editInfo").attr("disabled", true);
		}
	});
	$("#name").keyup(function(){
		var name1 = $("#name").val();
		if(validateName(name1) || name1 ==""){
			name = name1;
		}else{
			$("#name").val(name);
		}
	});
	$("#age").keyup(function(){
		var age1 = $("#age").val();
		if(validateNumber(age1) || age1 ==""){
			age = age1;
		}else{
			$("#age").val(age);
		}
	});
	$("#telephone").keyup(function(){
		var telephone1 = $("#telephone").val();
		if(validateNumber(telephone1) || telephone1 ==""){
			telephone = telephone1;
		}else{
			$("#telephone").val(telephone);
		}
	});
	$("#telephone").blur(function(){
		if(!validateTelephone($("#telephone").val()) && $("#telephone").val() != ""){
			alert("请输入正确的电话号码!");
		}
	});
});
function validateName(name){
	return (/^[A-Za-z\u4e00-\u9fa5_]+$/).test(name);
}
function validateNumber(number){
	return (/^[0-9]+$/).test(number);
}
function validateTelephone(telephone){
	return (/^((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)$/).test(telephone);
}
function preview(img, selection) {
    if (!selection.width || !selection.height)
        return;
    $('#x').val(selection.x1);
    $('#y').val(selection.y1);
    $('#width').val(selection.width);
    $('#height').val(selection.height);    
}
//上传图片
function ajaxFileUpload(fileId, x, y, width, height) {
	$.ajaxFileUpload
	(
		{
			url: "AcceptImg",
			secureuri: false,
			fileElementId: fileId,
			//data: {"x":x, "y":y, "width":width, "height":height},
			success: function (data, status)
			{
				var imgName = $(data).find("body").text();
				$.post("changeImg",
					{"x":x, "y":y, "width":width, "height":height, "imgName":imgName},
					function(result){
						if(result == "yes"){
							$("#photo").attr("src","img/"+imgName);
						}
					});
				$(".file").val("");
				$(".touxiang").css("box-shadow","none");
				$(".imgChange").css("display","block");
				$(".changeBtn, .btn-close1").css("display","none");
				$(".touxiang").css("height", "200px");
				$('#photo').imgAreaSelect({hide: 'true'});
				$("#height").val(85);
				$("#width").val(85);
				$("#x").val(58);
				$("#y").val(27);
			},
			error: function (data, status, e)
			{
				alert("上传出错");
			}
		}
	)
}